package com.zhxx.book.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhxx.book.entity.BookCourse;
import com.zhxx.book.entity.Course;
import com.zhxx.book.entity.Teacher;
import com.zhxx.book.entity.User;
import com.zhxx.book.service.IBookService;
import com.zhxx.book.service.ICourseService;
import com.zhxx.book.service.ITeacherService;
import com.zhxx.book.service.IUserService;
import com.zhxx.book.vo.BookDetailVo;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 预约课程
 * @Author Alex
 * @Date 2022-11-14 09:53
 */
@RestController
@RequestMapping("/action")
public class BookController {
   @Autowired
   private IBookService bookService;
   @Autowired
   private ICourseService courseService;
   @Autowired
   private IUserService userService;
   @Autowired
   private ITeacherService teacherService;

   @GetMapping
   public BookDetailVo list(@RequestParam Long courseId) {
      // 课程预定列表 课程名称
      BookDetailVo bookDetailVo = new BookDetailVo();
      Course course = courseService.getById(courseId);
      if(course == null) {
         return bookDetailVo;
      }

      Teacher teacher = teacherService.getById(course.getTeacherId());
      course.setTeacherName(teacher.getName());
      LambdaQueryWrapper<BookCourse> queryWrapper = Wrappers.lambdaQuery();
      queryWrapper.eq(BookCourse::getCourseId, courseId);
      List<BookCourse> bookCourseList = bookService.list(queryWrapper);
      List<Long> userIds = bookCourseList.stream().map(BookCourse::getUserId).collect(Collectors.toList());
      List<User> userList = new ArrayList<>();
      if(CollectionUtils.isNotEmpty(userIds)) {
         userList = userService.listByIds(userIds);
      }
      bookDetailVo.setCourse(course);
      bookDetailVo.setUsers(userList);
      return bookDetailVo;
   }

   @PostMapping
   public Boolean add(@RequestBody BookCourse bookCourse) {
      return bookService.save(bookCourse);
   }

   @PutMapping
   public Boolean update(@RequestBody BookCourse bookCourse) {
      return bookService.updateById(bookCourse);
   }

   @DeleteMapping("/{id}")
   public Boolean delete(@PathVariable Long id) {
      return bookService.removeById(id);
   }
}
